<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp"%>

<!DOCTYPE html>
<html>
<head>
    <title><spring:message code="message.lable.ShouHuoDiZhiGuanLi"/></title>
    <%@ include file="/WEB-INF/component/commonCSS.jsp"%>
</head>

<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">

    <section class="content-header">
        <h1><spring:message code="message.list.SheZhiPeiSongDiZhi"/></h1>
        <ol class="breadcrumb">
            <li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"/></a></li>
            <li class="active"><a href="#"><spring:message code="message.list.SheZhiPeiSongDiZhi"/></a></li>
        </ol>
    </section>
    
    <section class="content">
        <div class=" box box-success">
            <div class="box-body">
                <div class="input">
                    <div class="textalign1"><spring:message code="message.listForm.YongHuName"/><spring:message code="message.yanZheng.maoHao"/></div>
                        <div class="boxinput">
                        <input type="text" class="textinput" id="name" name="name" placeholder="<spring:message code="message.listForm.YongHuName"/>" />
                    	</div>
                    	<div class="textalign"><spring:message code="message.listForm.XiangXiSongHuoDiZhi"/><spring:message code="message.yanZheng.maoHao"/></div>
                        <div class="boxinput">
                        <input type="text" class="textinput" id="recpAddr" name="recpAddr" placeholder="<spring:message code="message.listForm.XiangXiSongHuoDiZhi"/>" />
                    	</div>
                    	<div class="textalign"><spring:message code="message.listForm.ShouHuoRen"/><spring:message code="message.yanZheng.maoHao"/></div>
                        <div class="boxinput">
                        <input type="text" class="textinput" id="recpUser" name="recpUser" placeholder="<spring:message code="message.listForm.ShouHuoRen"/>" />
                    	</div>
                    </div>
                    <div class="input" style="margin-top:3px">
                     <div class="textalign1"><spring:message code="message.lable.status"/><spring:message code="message.yanZheng.maoHao"/></div>
                     <div class="boxinput">
                        <select class="textinput"  id="status" name="status"> 
                            <option value=""><spring:message code="message.select.option"/></option>
                            <option value="1"><spring:message code="message.button.start"/></option>
                            <option value="0"><spring:message code="message.button.stop"/></option>
                        </select>
                    </div>
                    <div class="textalign" >
                    <button type="button" class="btn btn-success btn-sm" onclick="search()" id="selectDicCompType"><spring:message code="message.button.seachSpacing"/></button>
                    </div>
                </div>
            </div>
        </div>
        
        <table class="jqgrid" id="gridlist"></table>
        <div id="gridpage"></div>
        
        <div class=" box box-warning bottongroup">
             <a href="toAddressEdit.html?opType=0" class="btn btn-primary btn-sm"><spring:message code="message.button.addSpacing"/></a>
             <label class="btn btn-primary btn-sm" onclick="batchChangeStatus(1)"><spring:message code="message.button.startSpacing"/></label>
             <label class="btn btn-primary btn-sm" onclick="batchChangeStatus(0)"><spring:message code="message.button.stopSpacing"/></label>
             
        </div>
        
    </section>
    
    <%@ include file="/WEB-INF/component/commonJS.jsp"%>
    <script type="text/javascript">
  //绑定回车
    $('#name,#recpAddr,#recpUser,#status').bind('keydown', function(event) {
        if (event.keyCode == "13") {
            $('#selectDicCompType').click();
        }
    });	
    $("#name").focus();//加载页面光标在第一个查询框内
	
    
    
    
	    function showFocus(rowId) {
			$("#gridlist").resetSelection();
			$("#gridlist").setSelection(rowId);
		}
    
        $(function () {
            $("#gridlist").jqGrid({
                url: "getAddressList.html",
                mtype: "POST",
                datatype: "json",
                autowidth: true,
                height: 255,
                colNames: [
                           '<spring:message code="message.jqGrid.cz"/>',
                           '<spring:message code="message.listForm.YongHuName"/>',
                           '<spring:message code="message.lable.SongHuoDiZhiJianCheng"/>',
                           '<spring:message code="message.listForm.XiangXiSongHuoDiZhi"/>',
                           '<spring:message code="message.listForm.ShouHuoRen"/>', 
                           '<spring:message code="message.lable.ShouHuoRenDianHua"/>', 
                           '<spring:message code="message.lable.status"/>',
                           '<spring:message code="message.lable.ShiFouMoRenDiZhi"/>'
                           ],
                colModel: [
                    { name: 'operate', width: 40, align:"left", sortable: false,
                       formatter : function(val, opts, rowdata) {
                         var str =  "<a href='toAddressEdit.html?opType=1&id=" + rowdata.recpAddrId + "' style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat 0px 0px' class='opIcon' title='<spring:message code="message.button.edit"/>'></a>";
                          if(rowdata.isdefault == 0){
                         str += "<a href='javascript:setDefaultAddress(\""+rowdata.recpAddrId+"\", 1);' style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -60px -20px' class='opIcon' title='<spring:message code="message.button.SheWeiMoRenDiZhi"/>'></a>"
                           }
                           return str;
                    }},
                    { name: 'user.name', width: 100 , align:"center" ,sortable: false},
                    { name: 'recpAddrS', width: 80 , align:"center" ,sortable: false},
                    { name: 'recpAddr', width: 180 , align:"center" ,sortable: false},
                    { name: 'recpUser', width: 60 , align:"center" ,sortable: false},
                    { name: 'recpUserphone', width: 80 , align:"center",sortable: false },
                    { name: 'status', width: 30 , align:"center",sortable: false ,
                    	cellattr:function (rowId, val, rawObject, cm) {
                            if(val == '停用'){
                                return 'style="color:red"';
                            }
                        },
                    	formatter : function(val, opts, rowdata) {
                    		var strSta = "";
                    		if(rowdata.status == 1){
                    			strSta += "<spring:message code="message.button.start"/>";
                    		}else if(rowdata.status == 0){
                    			strSta += "<spring:message code="message.button.stop"/>";
                    		}
                    		return strSta;
                    	}
                     },
                    { name: 'isdefault', hidden:true, width: 30 , align:"center",sortable: false}
                    
                ],
                rowNum: 20,
                rowList: [10, 20,50,100],
                rownumbers: true,
                multiselect: true,
                pager: "#gridpage",
                viewrecords: true,
                gridComplete:function(){
                	//序号列宽度自适应（参数为jqgridID）
                    autoRNWidth("gridlist");
                	//数据只有一条时默认选中（参数为jqgridID）
                    selectByOneData("gridlist");
                },
                caption: "<spring:message code="message.list.SheZhiPeiSongDiZhiLieBiao"/> ",
                jsonReader: {
                    repeatitems: false,
                    id: "recpAddrId"
                }
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            $(window).trigger("resize");
        });
        
        /* 查询 */
        function search(page) {
            var recpAddr = $.trim($("#recpAddr").val());
            var name = $.trim($("#name").val());
            var recpUser =  $.trim($("#recpUser").val());
            var status = $.trim($("#status").val());
            $("#gridlist").jqGrid("clearGridData");
                $("#gridlist").jqGrid('setGridParam',{  
                mtype: "POST",
                datatype:'json',
                postData:{
                	"user.name":name,
                	"recpAddr":recpAddr,
                	"recpUser":recpUser,
                	"status" : status
                	},
                page : (page || 1)
            }).trigger("reloadGrid"); //重新载入  
        }
        
        /* 停用 启用 */
        function batchChangeStatus(status) {
        	var msg = "";
            if (status == 1){
                msg = "<spring:message code="message.button.start"/>";
            } else if (status == 0){
                msg = "<spring:message code="message.button.stop"/>";
            }
        	
        	
            var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
            if (rowIds.length <= 0) {
            	 $.alert("<spring:message code="message.lable.pleaseSelect"/>"+msg+"配送地址<spring:message code="message.yanZheng.ganTanHao"/>", "warn");
                 return;
            }
            
            var str = "";
            var names = "";
            var count = 0;
                for (var i = 0;i < rowIds.length; i++){
                    var rowdata = $("#gridlist").jqGrid('getRowData', rowIds[i]);
                    if (status == 1){
                        if (rowdata.status == "<spring:message code="message.button.stop"/>"){
                            names += rowdata.recpAddr+",";
                            str += rowIds[i]+",";
                            count++;
                        }
                    } else {
                        if (rowdata.status == "<spring:message code="message.button.start"/>"){
                            names += rowdata.recpAddr+",";
                            str += rowIds[i]+",";
                            count++;
                        }
                    }
                }
                if (names != ""){
                    names = names.substring(0,names.length-1);
                } else {
                    $.alert("<spring:message code="message.lable.ninXuanZe"/>配送地址<spring:message code="message.lable.yiJingShi"/>"+msg+"<spring:message code="message.lable.zhuangTaiGTH"/>", "warn");
                    return;
                }
            
           
			$.HN.message.confirm("<spring:message code="message.button.confirm"/>"+msg+"以下配送地址<spring:message code="message.lable.ma"/><br/>", '', '').on(function (e) {
	            if (e) {
            var list = [];
            for (var i=0; i<rowIds.length; i++) {
                rowId = rowIds[i];
                list.push({"recpAddrId":rowId, "status":status});
            }
            $.post("batchChangeStatus.html", {
                "array" : json.jsonToString(list)
            }, function(result) {
                if (result.success) {
                    var curpage = $("#gridlist").jqGrid('getGridParam', 'page');
                    search(curpage);
                    $.HN.message.alert("保存成功！", "<spring:message code="message.HN.alert.type"/>", "success");
                } else {
                    $.HN.message.alert("<spring:message code="message.HN.alert.baoCunBai"/>", "<spring:message code="message.HN.alert.type"/>", "error");
                }
            }, "json")
	            }
			});
        }
        
        /* 设置默认地址 */
         function setDefaultAddress(recpAddrId, isdefault) {
        	showFocus(recpAddrId);
        	$.HN.message.confirm("<spring:message code="message.yanZheng.QueDingSheWeiMoRenDiZhiMa"/>", "<spring:message code="message.HN.alert.type"/>", "<spring:message code="message.button.confirm"/>").on(function(bool) {
        		if (bool) {
        			$.post("setDefaultAddress.html", {
    	            	recpAddrId : recpAddrId,
    	                isdefault : isdefault
    	            }, function(result) {
    	                if (result.success) {
    	                    var curpage = $("#gridlist").jqGrid('getGridParam', 'page');
    	                    search(curpage);
    	                    $.HN.message.alert("<spring:message code="message.HN.alert.SheZhiCheng"/>", "<spring:message code="message.HN.alert.type"/>", "success");
    	                } else {
    	                    $.HN.message.alert("<spring:message code="message.HN.alert.SheZhiBai"/>", "<spring:message code="message.HN.alert.type"/>", "error");
    	                }
    	            }, "json");
        		}
        	});
        }
    </script>
</body>
</html>